package com.yatang.yiliao.app.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.yatang.yiliao.app.R;
import com.yatang.yiliao.app.adapters.FragmentAdapter;
import com.yatang.yiliao.app.fragments.AnonymityFragment;
import com.yatang.yiliao.app.fragments.MessageFragment;
import com.yatang.yiliao.app.fragments.TropicFragment;

import java.util.LinkedList;


public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private SlidingMenu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setMenu(R.layout.leftmenu);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_main);
        radioGroup.setOnCheckedChangeListener(this);

        viewPager = (ViewPager) findViewById(R.id.main_viewPager);
        LinkedList<Fragment> fragments = new LinkedList<Fragment>();
        Fragment fragment = new MessageFragment();
        fragments.add(fragment);
        fragment = new AnonymityFragment();
        fragments.add(fragment);
        fragment = new TropicFragment();
        fragments.add(fragment);

        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOnPageChangeListener(this);
        radioGroup.check(R.id.rb_messages);
        RadioButton radioButton = (RadioButton)findViewById(R.id.rb_messages);
        radioButton.setTextColor(Color.parseColor("#FFBA17"));
    }

    public  void sdOnClick(View view)
    {
        Toast.makeText(this, "awadaw", Toast.LENGTH_SHORT).show();
        menu.toggle();
        menu.showMenu();
    }
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        switch (i){
            case 0:
                radioGroup.check(R.id.rb_messages);
                RadioButton radioButton = (RadioButton)findViewById(R.id.rb_messages);
                radioButton.setTextColor(Color.parseColor("#FFBA17"));
                radioButton = (RadioButton)findViewById(R.id.rb_anonymity);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                radioButton = (RadioButton)findViewById(R.id.rb_tropic);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                break;
            case 1:
                radioGroup.check(R.id.rb_anonymity);
                radioButton = (RadioButton)findViewById(R.id.rb_anonymity);
                radioButton.setTextColor(Color.parseColor("#FFBA17"));
                radioButton = (RadioButton)findViewById(R.id.rb_tropic);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                radioButton = (RadioButton)findViewById(R.id.rb_messages);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                break;
            case 2:
                radioGroup.check(R.id.rb_tropic);
                radioButton = (RadioButton)findViewById(R.id.rb_tropic);
                radioButton.setTextColor(Color.parseColor("#FFBA17"));
                radioButton = (RadioButton)findViewById(R.id.rb_anonymity);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                radioButton = (RadioButton)findViewById(R.id.rb_messages);
                radioButton.setTextColor(Color.parseColor("#B0B0B0"));
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_messages:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rb_anonymity:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rb_tropic:
                viewPager.setCurrentItem(2);
                break;
        }
    }
}
